package com.ycm.repository;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycm.dto.UserPageQueryDto;
import com.ycm.entity.User;
import com.ycm.mapper.UserMapper;
import org.springframework.stereotype.Repository;

/**
 * 用户仓储
 *
 * @author ycm
 * @date 2024-11-13
 */
@Repository
public class UserRepository extends ServiceImpl<UserMapper, User> {
    /**
     * 查询用户信息
     *
     * @param name 用户名
     */
    public User getUserByName(String name) {
        return baseMapper.getUserByName(name);
    }


    /**
     * 分页查询用户列表
     */
    public IPage<User> getPageList(UserPageQueryDto dto) {
        Assert.notNull(dto);

        Page<User> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>lambdaQuery()
                .like(StrUtil.isNotBlank(dto.getName()), User::getName, dto.getName());

        return baseMapper.selectPage(page, queryWrapper);
    }
}
